package com.wangp.springbootsource.event;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * classname AbstractEventMulticaster
 * description
 * </pre>
 *
 * @author wangp
 * @date 2020/9/2 22:27
 **/
@Component
public abstract class AbstractEventMulticaster implements EventMulticaster {

    @Autowired
    List<WeatherListener> listeners;

    @Override
    public void multicast(WeatherEvent event) {
        doStart();
        listeners.forEach(e -> e.onWeatherEvent(event));
        doEnd();
    }

    protected abstract void doStart();

    protected abstract void doEnd();

    @Override
    public void addListener(WeatherListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeListener(WeatherListener listener) {
        listeners.remove(listener);
    }
}
